/**
 * 点评服务类
 * 用于提交点评、获取点评列表等点评相关操作
 */

const request = require('../libs/request');

class PingService {
	/**
	 * 提交点评
	 * @param {Object} params 点评参数
	 * @param {number} params.order_id 订单ID
	 * @param {number} params.rating 星级评分(1-5)
	 * @param {string} params.content 点评内容
	 * @param {Array} params.images 图片数组
	 * @param {boolean} params.showLoading 是否显示加载提示，默认true
	 * @returns {Promise<Object>} 返回提交结果
	 */
	static async submitPing(params = {}) {
		const { 
			order_id, 
			rating, 
			content = '', 
			images = [], 
			showLoading = true ,
			type
		} = params;

		if (!order_id) {
			return {
				success: false,
				data: null,
				message: '订单ID不能为空'
			};
		}

		if (!rating || rating < 1 || rating > 5) {
			return {
				success: false,
				data: null,
				message: '请选择星级评分(1-5星)'
			};
		}

		try {
			const response = await request.post('/v1/ping/submit', {
				showLoading,
				needAuth: true,
				data: {
					order_id,
					rating,
					content,
					images: JSON.stringify(images),
					type
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '点评提交成功'
			};
		} catch (error) {
			console.error('提交点评失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '提交点评失败'
			};
		}
	}

	/**
	 * 获取订单点评详情
	 * @param {Object} params 查询参数
	 * @param {number} params.order_id 订单ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回点评详情数据
	 */
	static async getPingDetail(params = {}) {
		const { order_id, showLoading = false } = params;

		if (!order_id) {
			return {
				success: false,
				data: null,
				message: '订单ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/ping/detail', {
				showLoading,
				needAuth: true,
				data: {
					order_id
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取点评详情成功'
			};
		} catch (error) {
			console.error('获取点评详情失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取点评详情失败'
			};
		}
	}

	/**
	 * 获取商家点评列表
	 * @param {Object} params 查询参数
	 * @param {number} params.biz_id 商家ID
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回点评列表数据
	 */
	static async getPingList(params = {}) {
		const { 
			biz_id, 
			page = 1, 
			pageSize = 10, 
			showLoading = false 
		} = params;

		if (!biz_id) {
			return {
				success: false,
				data: null,
				message: '商家ID不能为空'
			};
		}

		try {
			const response = await request.get('/v1/ping/list', {
				showLoading,
				needAuth: false,
				data: {
					biz_id,
					page,
					page_size: pageSize
				}
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取点评列表成功'
			};
		} catch (error) {
			console.error('获取点评列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取点评列表失败'
			};
		}
	}
}

module.exports = PingService; 